/**
 * Created by 曲艺 on 2016/01/13.
 */
define(['angular','app.directives','services/common/base','laydate'],function(angular, directives){
    'use strict';

    /**
     * 发布考试
     */

    directives.directive("laydateDir",[function(){
        return {
            require: '?ngModel',
            restrict : 'A',
            scope:{
                ngModel: '='
            },
            link : function(scope, element, attr,ngModel) {

                // Specify how UI should be updated
                ngModel.$render = function() {
                    element.val(ngModel.$viewValue || '');
                };
                // Listen for change events to enable binding
                element.on('blur keyup change', function() {
                    scope.$apply(read);
                });
                //曲：这么写是为了响应id为{{item.id}}的动态形式。不要写click，FF支持不好。
                element.on('focus', function () {
                    laydate({
                        elem :'#'+attr.id,
                        format:attr.format!=undefined&&attr.format!=''?attr.format:'YYYY-MM-DD hh:mm',
                        istime:attr.istime,
                        istoday:false,//迫不得已  （点击今天不会刷新modal）
                        choose:function(data){
                            scope.$apply(read);
                        }
                    });
                });
                read(); // initialize
                // Write data to the model
                function read() {
                    var val = element.val();
                    ngModel.$setViewValue(val);
                }
                if (attr.skin)
                    laydate.skin(attr.skin);

            }
        }
    }]);
});